package com.qf.mapper.impl;

import com.qf.entity.EbookEntry;
import com.qf.mapper.EbookEntryMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.io.InputStream;

@Repository
public class EbookEntryMapperImpl implements EbookEntryMapper {

    private SqlSession sqlSession;

    @Before
    public void init() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        sqlSession = factory.openSession();

    }

    @Override
    public Integer save(EbookEntry ebook) {
        EbookEntryMapper mapper = sqlSession.getMapper(EbookEntryMapper.class);
        return mapper.save(ebook);
    }

    @Override
    public Integer update(EbookEntry ebook) {
        EbookEntryMapper mapper = sqlSession.getMapper(EbookEntryMapper.class);
        return mapper.update(ebook);
    }
}
